package com.stm.outandstorage.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.util.StringUtil;
import com.stm.basicData.domain.KfToolsType;
import com.stm.basicData.mapper.KfToolsTypeMapper;
import com.stm.common.annotation.DataScope;
import com.stm.common.exception.BusinessException;
import com.stm.common.utils.DateUtils;
import com.stm.outandstorage.domain.KfToolsInoutRecords;
import com.stm.outandstorage.mapper.KfToolsInoutRecordsMapper;
import com.stm.outandstorage.service.KfToolsInoutRecordsService;
import com.stm.toolLedger.domain.KfTools;
import com.stm.toolLedger.mapper.KfToolsMapper;

/**
 * 
 * @Description RFIdimpl
 * @CreateTime 2019年4月4日 上午8:43:59
 * @CreateBy 林龙
 */
@Service
@Transactional
public class KfToolsInoutRecordsServiceImpl implements KfToolsInoutRecordsService
{
    
	@Autowired
	private KfToolsInoutRecordsMapper kfToolsInoutRecordsMapper;
	@Autowired
	private KfToolsMapper kfToolsMapper;
	@Autowired
	private KfToolsTypeMapper kfToolsTypeMapper;

    /**
     * 根据条件分页查询角色数据
     * 
     * @param kfToolsInoutRecords 出入库记录
     * @return 出入库记录集合信息
     */
    @Override
    @DataScope(tableAlias = "b5")
    public List<KfToolsInoutRecords> selectAllPageList(KfToolsInoutRecords kfToolsInoutRecords)
    {
        return kfToolsInoutRecordsMapper.selectAllPageList(kfToolsInoutRecords);
    }


    /**
     * 
     * @Description 查询出入库记录条数
     * @CreateTime 2019年4月18日 上午11:55:11
     * @CreateBy 林龙
     * @return
     */
	@Override
	public int selectByCount() {
		return kfToolsInoutRecordsMapper.selectByCount();
	}


	@Override
	public int insert(KfToolsInoutRecords kfToolsInoutRecords, String loginName) {
		String [] kfToollist=kfToolsInoutRecords.getKfToolList().split("##");
		int num=0;
		for (String kfToolsString : kfToollist) {
			String [] kfTools=kfToolsString.split(",");
			UUID uuid = UUID.randomUUID();
			kfToolsInoutRecords.setId(uuid.toString());
			kfToolsInoutRecords.setBarcode(kfTools[0]);
			kfToolsInoutRecords.setUnitid(kfTools[1]);
//			kfToolsInoutRecords.setUnitid(kfTools[2]);
			
			if(kfTools[2].equals("2")){
				throw new BusinessException("RFID为"+kfTools[0]+"的"+kfTools[3]+"已报废，不能领用或者出库!");
			}
			
			KfTools kfTools2 = kfToolsMapper.selectKfTools(kfTools[0]);
			KfTools kfTools1=new  KfTools();
			if(kfToolsInoutRecords.getStatus().equals("out")){//出库
				
				if(!kfTools2.getInoutState().equals("1")){
					throw new BusinessException("RFID为"+kfTools[0]+"的"+kfTools[3]+"已领用，不能重复领用!");
				}
				
				kfToolsInoutRecords.setOutOperator(loginName);
				kfToolsInoutRecords.setOutTime(DateUtils.getNowDate());
				kfToolsInoutRecords.setOptype("1");
				
				if(kfTools[2].equals("3")){//库存在库状态
					kfTools1.setInoutState("3");
					kfTools1.setStatus("3");
					num=3;//试验出库
				}else{
					
					kfTools1.setInoutState("2");
					num=1;//检修出库
				}
				
				
			}else{//入库
				if(kfTools2.getInoutState().equals("1")){
					throw new BusinessException("RFID为"+kfTools[0]+"的"+kfTools[3]+"已入库，不能重复入库!");
				}
				kfToolsInoutRecords.setInOperator(loginName);
				kfToolsInoutRecords.setInTime(DateUtils.getNowDate());
				kfToolsInoutRecords.setOptype("2");
				
				//库存在库状态
				kfTools1.setInoutState("1");
				
				if(kfTools[2].equals("3")){//库存在库状态
					kfTools1.setTestDate(DateUtils.getNowDate());
					
					KfToolsType kfToolsType=kfToolsTypeMapper.selectByPrimaryKey(kfTools2.getToolsTypeId());
		    		if(kfToolsType !=null && kfTools1.getTestDate() != null){
		    			Calendar cal = Calendar.getInstance();
					    cal.setTime(kfTools1.getTestDate());//设置起时间
					    cal.add(Calendar.MONTH,(kfToolsType.getTestCycle()));//工具类型的周期 
					    cal.add(Calendar.DATE, -1);
					    kfTools1.setNextTestDate(cal.getTime());
		    			
		    		}
		    		kfTools1.setStatus("1");
		    		num=4; //试验入库
				}else{
					num=2; //检修入库
				}
				
			}
			kfTools1.setRfid(kfTools[0]);
			
				
			kfToolsInoutRecordsMapper.insertSelective(kfToolsInoutRecords);
			
			//根据rfid来修改工具数据
			kfToolsMapper.updateByPrimaryRfid(kfTools1);
				
			
		}
		return num;
		
	}


	@Override
	@DataScope(tableAlias = "b2")
	public List<Map<String, Object>> getChartData(KfToolsInoutRecords kfToolsInoutRecords) {
		List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
		
		List<KfToolsInoutRecords> list1 = kfToolsInoutRecordsMapper.selectAllChartData(kfToolsInoutRecords);

		for (KfToolsInoutRecords kfToolsInoutRecords1 : list1) {
			Map<String,Object> resultMap = new HashMap<String,Object>();// 返回值
			
			 Calendar a = Calendar.getInstance();
			 a.set(Calendar.DATE, 1);
			 a.roll(Calendar.DATE, -1);
			 int maxDate = a.get(Calendar.DATE);

			
			if(kfToolsInoutRecords1.getInTime() != null){//入库
				long num=DateUtils.getDateNum(DateUtils.getNowDate(),kfToolsInoutRecords1.getInTime());
				
				for (int i = 0; i < maxDate; i++) {
					if(num == i){
						if(StringUtil.isNotEmpty(kfToolsInoutRecords1.getInOperator())){
							resultMap.put("equipmenthisSd", 1); //入库数量
						}
						resultMap.put("equipmenthisDate", kfToolsInoutRecords1.getInTime()); //周
					}
				}
				
				
			}
			if(kfToolsInoutRecords1.getOutTime() != null){
				long num1=DateUtils.getDateNum(DateUtils.getNowDate(),kfToolsInoutRecords1.getOutTime());
				
				for (int i = 0; i < maxDate; i++) {
					if(num1 == i){
						if(StringUtil.isNotEmpty(kfToolsInoutRecords1.getOutOperator())){
							resultMap.put("equipmenthisWd", 1); //出库数量
						}
						resultMap.put("equipmenthisDate", kfToolsInoutRecords1.getOutTime()); //周
					}
				}
	
			}
			
			resultList.add(resultMap);
		}

		return resultList;
	}


	@Override
	@DataScope(tableAlias = "b2")
	public List<Map<String, Object>> getChartztData(KfToolsInoutRecords kfToolsInoutRecords) {
		List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
		
		List<KfToolsInoutRecords> list1 = kfToolsInoutRecordsMapper.selectChartztData(kfToolsInoutRecords);

		for (KfToolsInoutRecords kfToolsInoutRecords1 : list1) {
			Map<String,Object> resultMap = new HashMap<String,Object>();// 返回值
			resultMap.put("name", kfToolsInoutRecords1.getToolName());
			resultMap.put("toolsNums", kfToolsInoutRecords1.getToolsNums());
			
			
			resultList.add(resultMap);
		}

		return resultList;
	}

   

}
